package com.itheima.d5_synchronized_method;

public class Ticked implements Runnable {
    private int ticket = 100;

    @Override
    public void run() {
        while (true) {
            boolean result = sellTicked();
            if (result) {
                break;
            } else {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }


    public synchronized boolean sellTicked() {

        if (ticket <= 0) {
            return true;
        } else {
            ticket--;
            System.out.println(Thread.currentThread().getName() + "在卖票，还剩余" + ticket + "张票");
            return false;
        }
    }
}